package com.senseuni.common.dialog.indicator;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

/**
 * 实现一个简单的圆形加载进度View
 * 1. 有个边框
 * 2. 进度采用扇形+文字形式
 */
public class CircleProgressView extends View {

    private static final int DEFAULT_MAX = 100;
    private static final int DEFAULT_PROGRESS = 0;
    private static final int DEFAULT_START_ANGLE = -90;
    private static final float DEFAULT_STROKE_WIDTH = 3f;
    private static final float DEFAULT_TEXT_SIZE = 14f;
    private static final int DEFAULT_VIEW_SIZE = 96;

    private static final int DEFAULT_PROGRESS_COLOR = 0x80ffffff;
    private static final int DEFAULT_TEXT_COLOR = 0xffffff;

    private float mStrokeWidth = DEFAULT_STROKE_WIDTH;
    private float mTextSize = DEFAULT_TEXT_SIZE;
    private int mViewSize = DEFAULT_VIEW_SIZE;

    private DisplayMetrics mDisplayMetrics;

    private Paint mStrokePaint;
    private Paint mTextPaint;
    private Paint mProgressPaint;

    private RectF mInnerRectF;

    private int mProgress = DEFAULT_PROGRESS;
    private int mMax = DEFAULT_MAX;
    private String mText = "";

    public CircleProgressView(Context context) {
        super(context);
        initView(context);
    }

    public CircleProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public CircleProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        mDisplayMetrics = context.getResources().getDisplayMetrics();
        mStrokeWidth = mStrokeWidth * mDisplayMetrics.density;
        mTextSize = mTextSize * mDisplayMetrics.density;

        mProgressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mProgressPaint.setColor(DEFAULT_PROGRESS_COLOR);
        mProgressPaint.setStyle(Paint.Style.FILL);

        mStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mStrokePaint.setColor(DEFAULT_PROGRESS_COLOR);
        mStrokePaint.setStyle(Paint.Style.STROKE);
        mStrokePaint.setStrokeWidth(mStrokeWidth);

        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setColor(DEFAULT_TEXT_COLOR);
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setTextAlign(Paint.Align.CENTER);

        mInnerRectF = new RectF();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width =resolveSize(DEFAULT_VIEW_SIZE, widthMeasureSpec);
        int height = resolveSize(DEFAULT_VIEW_SIZE, heightMeasureSpec);
        mViewSize = Math.min(width, height);
        setMeasuredDimension(width, height);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        mInnerRectF.set(0, 0, mViewSize, mViewSize);
        mInnerRectF.offset((getWidth() - mViewSize) / 2, (getHeight() - mViewSize) / 2);
        final int halfBorder = (int) (mStrokePaint.getStrokeWidth() / 2f + 0.5f);
        mInnerRectF.inset(halfBorder, halfBorder);
        float centerX = mInnerRectF.centerX();
        float centerY = mInnerRectF.centerY();
        float sweepAngle = 360 * mProgress / mMax;
        canvas.drawArc(mInnerRectF, -90, sweepAngle, true, mProgressPaint);

        int xPos = (int) centerX;
        int yPos = (int) (centerY - (mTextPaint.descent() + mTextPaint.ascent()) / 2);
        canvas.drawText(mText, xPos, yPos, mTextPaint);
        //画边框
        canvas.drawOval(mInnerRectF, mStrokePaint);
    }

    public int getMax() {
        return mMax;
    }

    /**
     * Sets the maximum progress value. Defaults to 100.
     */
    public void setMax(int max) {
        if (max <= 0 || max < mProgress) {
            throw new IllegalArgumentException(
                    String.format("Max (%d) must be > 0 and >= %d", max, mProgress));
        }
        mMax = max;
        invalidate();
    }

    /**
     * Gets the current progress from 0 to max.
     */
    public int getProgress() {
        return mProgress;
    }

    /**
     * Sets the current progress (must be between 0 and max).
     */
    public void setProgress(int progress) {
        if (progress > mMax || progress < 0) {
            throw new IllegalArgumentException(
                    String.format("Progress (%d) must be between %d and %d", progress, 0, mMax));
        }
        mProgress = progress;
        invalidate();
    }

    /**
     * Gets the text of the view.
     */
    public String getText() {
        return mText;
    }

    /**
     * Sets the text of the view.
     * @param text to be displayed in the view
     */
    public void setText(String text) {
        mText = text;
        invalidate();
    }

}
